﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace TeamCommunitySolution.Helpers
{
    /// <summary>
    /// Paginated List
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PaginatedList<T> : List<T>
    {
        /// <summary>
        /// Internal holder.
        /// </summary>
        public int PageIndex { get; private set; }

        /// <summary>
        /// Internal holder.
        /// </summary>
        public int PageSize { get; private set; }

        /// <summary>
        /// Internal holder.
        /// </summary>
        public int TotalCount { get; private set; }

        /// <summary>
        /// Internal holder.
        /// </summary>
        public int TotalPages { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="PaginatedList&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            this.SetPagination(source, pageIndex, pageSize);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PaginatedList&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        public PaginatedList(IList<T> source, int pageIndex, int pageSize)
        {
            this.SetPagination(source.AsQueryable(), pageIndex, pageSize);
        }

        private void SetPagination(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

        /// <summary>
        /// Gets a value indicating whether this instance has previous page.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has previous page; otherwise, <c>false</c>.
        /// </value>
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 0);
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance has next page.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has next page; otherwise, <c>false</c>.
        /// </value>
        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 < TotalPages);
            }
        }
    }

}
